package com.wy.System.Print.ideaControlConsole;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

public class DrawStar extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // 设置绘图环境
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0, 0, getWidth(), getHeight());

        // 星星中心点坐标
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        // 星星的大小
        int size = 100;

        // 星星的顶点坐标
        int[] xPoints = new int[10];
        int[] yPoints = new int[10];

        for (int i = 0; i < 10; i++) {
            // 每个顶点的角度
            double angle = (i * 36) * Math.PI / 180.0;
            // 奇数顶点是小顶点
            int offset = (i % 2 == 0) ? size / 2 : size / 4;
            // 计算顶点坐标
            xPoints[i] = (int) (centerX + offset * Math.cos(angle));
            yPoints[i] = (int) (centerY - offset * Math.sin(angle));
        }

        // 创建多边形并绘制
        Polygon star = new Polygon(xPoints, yPoints, 10);
        g2d.setColor(Color.YELLOW);
        g2d.fill(star);
    }

    //请注意，这里我们使用了fill方法来填充星星形状，如果你想要一个空心星星，可以使用drawPolygon方法代替
    public static void main(String[] args) {
        JFrame frame = new JFrame("Draw Star Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);
        frame.add(new DrawStar());
        frame.setVisible(true);
    }
}
